﻿import { Component, Injector, ViewChild} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { StandardPartsFeatureDto, UsableRangeDto,RequirementChangeHistoryState } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { RequirementPackageDto,TestDataServiceProxy,RequirementPackageState,RequirementProductionServiceProxy,RequirementForModelDto } from '@shared/service-proxies/service-proxies';
import { AbpSessionService } from '@abp/session/abp-session.service';
import { RequirementChangeHistoryRecord } from './requirementchangehistoryrecord.model';

@Component({
    templateUrl: './requirement-manager-audit.component.html',
    styleUrls: ['./requirement-manager-audit.component.less'],
    animations: [appModuleAnimation()]
})
export class RequirementManagerAuditComponent extends AppComponentBase{
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    tabObject:{
        filterText:""
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';
    hidden = true;
    selectAll:boolean=true;

    constructor(
        injector: Injector,
        private _pTestDataServiceProxy: TestDataServiceProxy,
        private _requirementProductionServiceProxy: RequirementProductionServiceProxy,
        private _abpSessionService :AbpSessionService) {
        super(injector);
    }

    clearTab(): void {

    }

    createOrEditRequirementPackage(requirementPackage: RequirementPackageDto): void {
        
    }
    deleteRequirementPackage(record :RequirementChangeHistoryRecord): void {
        this.message.confirm(
            '需求更改将被删除',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._requirementProductionServiceProxy.deleteRequirementChange(record.id)
                    .pipe(finalize(() => { this.getRequirementPackages();}))
                    .subscribe(() => {
                        this.notify.info(this.l('DeletedSuccessfully'));
                    });
                }
            }
        );
    }
    getRequirementPackages(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();

        this._requirementProductionServiceProxy.getRequirementChangeHistoryList(
            RequirementChangeHistoryState.审批,
            this.filterText,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                let list = Array<RequirementChangeHistoryRecord>();
                for(var key in result.items){
                    let rchr :RequirementChangeHistoryRecord = new RequirementChangeHistoryRecord();
                    rchr.id = result.items[key].id;
                    rchr.changer = result.items[key].changer;
                    rchr.reason = result.items[key].reason;
                    rchr.requirementForModelId = result.items[key].requirementForModelId;
                    rchr.state = result.items[key].state;
                    rchr.variationValue = result.items[key].variationValue;
                    //增加创建时间 2020-05-03 by songshn
                    rchr.creationTime = result.items[key].creationTime;
                    //工艺余量更改数 2020-05-03 by songshn
                    rchr.changedOfCraft = result.items[key].changedOfCraft;
                    rchr.requirementForModelDto = new RequirementForModelDto();
                    rchr.requirementForModelDto.usableRange = new UsableRangeDto();
                    rchr.requirementForModelDto.features = new StandardPartsFeatureDto();

                    list.push(rchr);
                    // 补充需求信息
                    this.GetRequirementForTask(rchr);
                }
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = list;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    GetRequirementForTask(record: RequirementChangeHistoryRecord) {
        this._requirementProductionServiceProxy.getRequirementForTask(record.requirementForModelId)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                record.requirementForModelDto = result;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    getRequirementChangeHistoryStateString(state:number):string{
        return RequirementChangeHistoryState[state];
    }
    /**
     * 需求包提交审核
     * @param record 
     */
    agreePackage(record: RequirementChangeHistoryRecord) {
        this.primengTableHelper.showLoadingIndicator();
        this._requirementProductionServiceProxy.agreedRequirementChange(record.id)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.primengTableHelper.hideLoadingIndicator();
                this.getRequirementPackages();
            });
    }
    /**
     * 需求更改审核撤销
     * @param record 
     */
    cancelApplyPackage(record: RequirementChangeHistoryRecord) {
        this.primengTableHelper.showLoadingIndicator();
        this._requirementProductionServiceProxy.cancelRequirementChange(record.id)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.primengTableHelper.hideLoadingIndicator();
                this.getRequirementPackages();
            });
    }
    getRequirementPackageStateString(state:number):string{
        return RequirementPackageState[state];
    }

    downloadWebLogs = function () {
        const self = this;
        self._webLogService.downloadWebLogs().subscribe((result) => {
            self._fileDownloadService.downloadTempFile(result);
        });
    };

    fixWebLogsPanelHeight(): void {

    }

    onResize(event): void {
        this.fixWebLogsPanelHeight();
    }

    ngOnInit(): void {
        const self = this;
    }
}
